package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.constant.FilePathConstant;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.content.entity.DocDirectory;
import com.dhcc.bpm.modules.content.entity.DocManagement;
import com.dhcc.bpm.modules.content.mapper.DocDirectoryMapper;
import com.dhcc.bpm.modules.content.service.IDocDirectoryService;
import com.dhcc.bpm.modules.content.service.IDocManagementService;
import com.dhcc.bpm.modules.content.vo.DecompressQueryVo;
import com.dhcc.bpm.modules.content.vo.DocManagementVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;


/**
 * @ClassName DocManagementController
 * @Description 文档目录 控制器
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("bpm-content/docmanagement")
@Api(value = "文档管理", tags = "文档管理接口")
public class DocManagementController extends BladeController {
	private static Logger log = LoggerFactory.getLogger(DocManagementController.class);

    private String decompressUrl;
    @Value("${bpm.file-public-path}")
    public void setFileUploadPath(String publicPath) {
        decompressUrl = publicPath + FilePathConstant.ZIPPATH;
    }

    @Autowired
    private IDocManagementService docManagementService;
    @Autowired
    private IDocDirectoryService docDirectoryService;
    @Autowired
    private DocDirectoryMapper docDirectoryMapper;

    /**
     * @param docManagement
     * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.content.entity.DocManagement>
     * @Description 详情
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入docManagement")
    @Log(title = "详情 文档管理", businessType = BusinessType.SELECT)
    @ApiLog("详情 文档管理")
    public R<DocManagement> detail(DocManagement docManagement) {
        DocManagement detail = docManagementService.getOne(Condition.getQueryWrapper(docManagement));
        return R.data(detail);
    }


    /**
     * @param docManagement
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage < com.dhcc.bpm.modules.content.entity.DocManagement>>
     * @Description 分页 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docName", value = "文档名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "docCode", value = "文档编号", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "opcDirectoryCode", value = "文档目录编号", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 2)
    @Log(title = "分页 文档管理", businessType = BusinessType.SELECT)
    @ApiLog("分页 文档管理")
    @ApiOperation(value = "分页", notes = "传入docManagement")
    public R<IPage<DocManagement>> list(@ApiIgnore @RequestParam Map<String, Object> docManagement, Query query) {
        IPage<DocManagement> pages = docManagementService.page(Condition.getPage(query), Condition.getQueryWrapper(docManagement, DocManagement.class));
        return R.data(pages);
    }

    /**
     * @param docManagement
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage < com.dhcc.bpm.modules.content.vo.DocManagementVO>>
     * @Description 自定义分页 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @Log(title = "分页 文档管理", businessType = BusinessType.SELECT)
    @ApiLog("分页 文档管理")
    @ApiOperation(value = "分页", notes = "传入docManagement")
    public R<IPage<DocManagementVO>> page(DocManagementVO docManagement, Query query) {
        IPage<DocManagementVO> pages = docManagementService.selectDocManagementPage(Condition.getPage(query), docManagement);
        return R.data(pages);
    }

    /**
     * @param docManagement
     * @return org.springblade.core.tool.api.R
     * @Description 新增 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入docManagement")
    @Log(title = "新增 文档管理", businessType = BusinessType.INSERT)
    @ApiLog("新增 文档管理")
    public R save(@Valid @RequestBody DocManagement docManagement) {
        return R.status(docManagementService.save(docManagement));
    }

    /**
     * @param docManagement
     * @return org.springblade.core.tool.api.R
     * @Description 修改 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入docManagement")
    @Log(title = "删除 文档管理", businessType = BusinessType.UPDATE)
    @ApiLog("删除 文档管理")
    public R update(@Valid @RequestBody DocManagement docManagement) {
        return R.status(docManagementService.updateById(docManagement));
    }

    /**
     * @param docManagement
     * @return org.springblade.core.tool.api.R
     * @Description 新增或修改 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入docManagement")
    @Log(title = "删除 文档管理", businessType = BusinessType.INSERT)
    @ApiLog("删除 文档管理")
    public R submit(@Valid @RequestBody DocManagement docManagement) {
        return R.status(docManagementService.saveOrUpdate(docManagement));
    }


    /**
     * @param ids
     * @return org.springblade.core.tool.api.R
     * @Description 删除 文档管理
     * @Author daina
     * @Date 10:50 2020/3/26
     **/
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "删除 文档管理", notes = "传入ids")
    @Log(title = "删除 文档管理", businessType = BusinessType.DELETE)
    @ApiLog("删除 文档管理")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(docManagementService.deleteLogic(Func.toLongList(ids)));
    }


    /**
     * @param opcDirectoryCode
     * @param id
     * @return org.springblade.core.tool.api.R
     * @Description 文档发布
     * @Author daina
     * @Date 10:51 2020/3/26
     **/
    @PostMapping("/release")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "文档发布", notes = "传入文档目录编号")
    @Log(title = "文档发布", businessType = BusinessType.UPLOAD)
    @ApiLog("文档发布")
    public R release(@ApiParam(value = "主键", required = true) @RequestParam String opcDirectoryCode, @ApiParam(value = "主键id", required = true) @RequestParam Long id) {
        //DocManagement docManagement = docManagementService.seleDocByCode(opcDirectoryCode);
        DocManagement docManagement = docManagementService.getById(id);
        Long opcPluploadId = docManagement.getOpcPluploadId();
        // 获取文件路径
        DecompressQueryVo queryVo = docManagementService.selectFilePathById(opcPluploadId);
        if (queryVo != null) {
            Long pluploadId = queryVo.getId();
            String name = queryVo.getFileRealName();
            String path = queryVo.getFileFullPath();
            //从文件服务器上下载zip并保存到本地
            if (!StringUtils.isNotEmpty(path)) {
                return R.status(false);
            }
            //downloadAndSaveZip(path,name);
            // 解压缩
            docManagementService.decompress(decompressUrl, name);
            //文档格式检验
            if (!docManagementService.checkDocFile(decompressUrl)) {
                return R.status(false);
            }
            //维护数据库数据
            int count = docDirectoryService.synInsertDirectory(decompressUrl, opcDirectoryCode);
            if (count <= 0) {
                return R.status(false);
            }
            //删除文件夹中下载的文件和解压的文件
            docManagementService.deleteDirectory(decompressUrl);

            //发送请求到门户端进行文档更新
            //String url = "http://" +  portUrl + "/release/"+pluploadId;
            //log.info(url);
            return R.status(true);
        }

        return R.status(false);
    }

    /**
     * @param docManagement
     * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.content.entity.DocManagement>
     * @Description 详情
     * @Author daina
     * @Date 10:51 2020/3/26
     **/
    @GetMapping("/detailSelf")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "详情", notes = "传入docManagement")
    @Log(title = "详情", businessType = BusinessType.SELECT)
    @ApiLog("详情")
    public R<DocManagement> detailSelf(DocManagement docManagement) {
        DocManagementVO detail = docManagementService.getDetailSelfById(docManagement.getId());
        return R.data(detail);
    }

    /**
     * @param opcDirectoryCode
     * @param id
     * @return org.springblade.core.tool.api.R
     * @Description 文档发布word
     * @Author daina
     * @Date 10:51 2020/3/26
     **/
    @PostMapping("/docRelease")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "文档发布", notes = "传入文档目录编号")
    @ApiLog("文档发布")
    @Log(title = "文档发布", businessType = BusinessType.INSERT)
    public R docRelease(@ApiParam(value = "主键", required = true) @RequestParam String opcDirectoryCode, @ApiParam(value = "主键id", required = true) @RequestParam Long id) throws Throwable {
        DocManagement docManagement = docManagementService.getById(id);
        Long opcPluploadId = docManagement.getOpcPluploadId();
        // 获取文件路径和名称
        DecompressQueryVo queryVo = docManagementService.selectFilePathById(opcPluploadId);
        if (queryVo != null) {
            Long pluploadId = queryVo.getId();
            String name = queryVo.getFileRealName();
            String path = queryVo.getFileFullPath();
            //修改维护文档请求地址
            DocDirectory docDirectory = docDirectoryMapper.selectParentByChildcode(opcDirectoryCode);
            DocDirectory directory = new DocDirectory();
            directory.setId(docDirectory.getId());
            directory.setUrl(path);
            docDirectoryService.updateById(directory);
            return R.status(true);
        }
        return R.status(false);
    }


}

